package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.listener.MtsxfExcelListener;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyDy;
import cn.stylefeng.guns.sys.modular.system.entity.BtyMtsxf;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyDyMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyDyParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyMtsxfParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyDyResult;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyMtsxfResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyDyService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 控制器
 *
 * @author liuy
 * @Date 2022-01-24 16:24:45
 */
@Controller
@RequestMapping("/btyDy")
public class BtyDyController extends BaseController {

    private String PREFIX = "/modular/system/btyDy";

    @Autowired
    private BtyDyService btyDyService;
    @Resource
    private BtyDyMapper dyMapper;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2022-01-24
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btyDy.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2022-01-24
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyDy_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2022-01-24
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyDy_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2022-01-24
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyDyParam btyDyParam) {
        this.btyDyService.add(btyDyParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2022-01-24
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyDyParam btyDyParam) {
        this.btyDyService.update(btyDyParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2022-01-24
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyDyParam btyDyParam) {
        this.btyDyService.delete(btyDyParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2022-01-24
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyDyParam btyDyParam) {
        BtyDy detail = this.btyDyService.getById(btyDyParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2022-01-24
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyDyParam btyDyParam) {
        return this.btyDyService.findPageBySpec(btyDyParam);
    }


    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUsers(@RequestParam("file") MultipartFile file, BtyDyParam param) {
        JsonResult jsonResult = new JsonResult();
        if(ToolUtil.isEmpty(param.getZdrq())){
            jsonResult.setState(0);
            jsonResult.setErrmsg("请先选择制单日期！");
            return jsonResult;
        }
        String fileName = file.getOriginalFilename();
        Boolean flag;
        try {
            flag = btyDyService.batchImport(fileName, file,param);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return flag;
    }
    @RequestMapping("/listSearchExport")
    @ResponseBody
    public List<BtyDyResult> listSearchExport (BtyDyParam param){
        List<BtyDyResult> list= btyDyService.customList(param);
        return list;
    }

}


